// pages/myshop/index.js
var app = getApp()
var c = require("../common.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: 0,
    select:true,
    id:null,
  selectnum:0,
  date_extend:null,
  curit:1,//改变当前显示视频，直播，预约，课件
  video:[],//存放视频数组
  zhibo:[],//存放直播数组
  yuyue:[]//存放预约数组
  },
  //改变当前显示视频，直播，预约，课件
  changeit(e){
  this.setData({
    curit:e.currentTarget.dataset.it
  })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
   var id=e.class_id//获取传进来的房间id
   this.setData({id:id});
   this.loadvideo(id)//加载房间的视频
   this.loadzhibo(id)//加载房间的直播
   this.loadyuyue(id,0)//加载房间的预约直播
   this.loadclassinfo(id)//加载房间的详情 
  },
  //加载房间的视频
  loadvideo(id){
  c.ajax(
    app.globalData.rootPath+"/index.php/index/Wxapi/loadvideobyid",
    {id:id},
    res=>{
     this.setData({
       video:res.data
     }) 
    }
  )
  },
  //加载房间的直播
  loadzhibo(id){
    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/loadzhibobyid",
      { id: id },
      res => {
        this.setData({
          zhibo: res.data
        })
      }
    )
  },
  //加载房间的预约直播

  loadyuyue(id,index){
    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/loadyuyuebyid",
      { id: id, index:index},
      res => {
        this.setData({
          yuyue: res.data.res,
          date_extend: res.data.date_extend
        }),
          console.log(res.data);
      }
    )
  },
  //加载房间的详情 
  loadclassinfo(id){
    var a = wx.getStorageSync("userInfo")
    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/getclassinfo",
      {
        class_id: id,
        uid: a.user_id },
      res => {
        this.setData({
          classinfo: res.data
        })
      }
    )
  },
  //关注或取消关注
  follow(e){
  var flag=e.currentTarget.dataset.isfollow
  var a=wx.getStorageSync("userInfo")
  c.ajax(
    app.globalData.rootPath+"/index.php/index/Wxapi/follow",
    {
    uid:a.user_id,
    class_id:this.data.classinfo.class_id,
    flag:flag
    },
    res=>{
      var data=this.data.classinfo
      flag ? data.isfollow = 0 : data.isfollow = 1
      this.setData({
        classinfo:data
      })
    }
  )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  //点击日期来筛选数据和背景
  dayinfo:function(e){
    var id=this.data.id;
    var select=e.currentTarget.dataset.id;
    this.loadyuyue(id, select);
  },
  
  videoclick:function(e){
    var that    = this;
    var yuyue   = this.data.yuyue;
    var vid     = e.target.dataset.vid;
    var select_goods_id = e.target.dataset.goodsid;
    var money = e.target.dataset.money;
    this.setData({ money: money, select_goods_id: select_goods_id});
    for (var $i = 0; $i <= yuyue.length-1;$i++){
      var up = "yuyue[" + $i + "].select";//先用一个变量，把(info[0].gMoney)用字符串拼接起来
      that.setData({
        [up]: true
      })
    }
    var up = "yuyue[" + vid + "].select";//先用一个变量，把(info[0].gMoney)用字符串拼接起来
    that.setData({
      [up]: false
    })
    console.log(yuyue);
  },
  suborder:function(){
    var goods_id = this.data.select_goods_id;
    var money=this.data.money;
    var yuyue=1;
    wx.navigateTo({
      url: '/pages/ordersure/ordersure?goods_id=' + goods_id + '&money=' + money + '&yuyue=' + yuyue,
    })
  },
  inDetail: function (e) {
    // 商品类型：

    // 1 -> 一对一直播
    // 2 -> 单节视频
    // 3 -> 套课
    // 4 -> 套课中的视频
    // 5 -> 预约直播
    // 6 -> 一对多直播
    var gid = e.currentTarget.dataset.goodsid;
    var sql = 'w(goods_id=' + gid + ')f(type)l(1)';
    c.ajax(
      app.dbs + 'goods',
      { sql: sql },
      res => {
        var typeinfo = res.data.type;
        console.log(typeinfo);
        if (typeinfo == 2) {
          wx.navigateTo({
            url: '/pages/detailsdanke/detailsdanke?goods_id=' + gid,
          })
        } else if (typeinfo == 3) {
          wx.navigateTo({
            url: '/pages/detailtaoke/detailtaoke?goods_id=' + gid,
          })
        } else if (typeinfo == 1 || typeinfo == 6) {
          wx.navigateTo({
            url: '/pages/detailzhibo/detailzhibo?goods_id=' + gid,
          })
        }
      }
    )
  }
})